package tag.in;

import java.util.Stack;

import tag.in.data.Tag;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


/**
 * Extend Application for global state information for an application. Access the application via
 * Activity.getApplication().
 * 
 * There are several ways to store global state information, this is one of them. Another is to
 * create a class with static members and just access it from Activities.
 * 
 * Either approach works, and there is debate about which is better. Either way, make sure to clean
 * up in life-cycle pause or destroy methods if you use resources that need cleaning up (static
 * maps, etc).
 * 
 * @author andre
 */
public class TagInApplication extends Application {
    private Tag currentTag = null;
    private double tagLat = 0;
    private double tagLon = 0;
    private int tagRadious = 50;
    private String tagAction = null;
    private String tagType = null;
    private int tagCategory = 0;
    private String tagName = null;
    private String tagOwner = null;
	private String[] tagCategories;
	private String currentImg = null;
	private Stack<Intent> intentlist = new Stack<Intent>();
	
	private String username = null;
	private String serverUrl = "willyou.bounceme.net"; //"10.0.2.2"; 192.168.1.101;
	//private String serverUrl = "10.0.2.2";
	//private String serverUrl = "192.168.1.101";
	private String serverPort = "8080";

    public TagInApplication() {
        super();   
    }
    

    /*
     * LocationListener Callback
     */
    private void updateWithNewLocation(Location location) {
		String latLongString;
		//TextView myLocationText;
		//myLocationText = (TextView)findViewById(R.id.myLocationText);
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			latLongString = "Lat:" + lat + "\nLong:" + lng;
			
			this.setTagLat(lat);
			this.setTagLon(lng);
		} else {
			latLongString = "No location found :(";
		}
		
		Log.v(Constants.LOGTAG, " TagInApplication === "+latLongString);
		//myLocationText.setText("Your Current Position is:\n" +
			//	latLongString);
		
	}
    
    /*
     * Listener to force coordinates update when changed
     */
	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);
		}
		public void onProviderDisabled(String provider){
			updateWithNewLocation(null);
		}
		public void onProviderEnabled(String provider){ }
		public void onStatusChanged(String provider, int status,
				Bundle extras){ }
	};
	
	public void startGpsListener() {
		/*
         * GPS Coordinates
         */
        LocationManager locationManager;
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager)getSystemService(context);

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		String provider = locationManager.getBestProvider(criteria, true);

		//String provider = LocationManager.GPS_PROVIDER;
		Location location =
			locationManager.getLastKnownLocation(provider);
		updateWithNewLocation(location);
		
		locationManager.requestLocationUpdates(provider, 2000, 10,
				locationListener);
	}
	
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }
    
    public void setCurrentTag(Tag currentTag) {
        this.currentTag = currentTag;
    }
        
    public Tag getCurrentTag() {
        return this.currentTag;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
        
    public String getUsername() {
        return this.username;
    }
    
    public int getTagCategory() {
        return this.tagCategory;
    }

    public double getTagLon() {
        return this.tagLon;
    }
    
    public void setTagLon(Double tagLon) {
        this.tagLon = tagLon;
    }
    
    public double getTagLat() {
        return this.tagLat;
    }
    
    public void setTagLat(Double tagLat) {
        this.tagLat = tagLat;
    }
    
    public String getTagName() {
        return this.tagName;
    }
    
    public int getTagRadious() {
        return this.tagRadious;
    }
    
    public void setTagRadious(int tagRadious) {
        this.tagRadious = tagRadious;
    }
    
    public void setTag(Tag currentTag) {
        this.currentTag = currentTag;
    }

    public void setTagCategory(int tagCategory) {
        this.tagCategory = tagCategory;
    }
    
    public void setTagName(String tagName) {
        this.tagName = tagName;
    }
    
    public void setTagAction(String tagAction) {
        this.tagAction = tagAction;
    }
    
    public void setTagType(String tagType) {
        this.tagType = tagType;
    }
    
    public String getTagAction() {
        return this.tagAction;
    }
    
    public String getTagType() {
        return this.tagType;
    }
    
    public String getTagOwner() {
    	return this.tagOwner;
    }
    
    public void setTagOwner(String tagOwner) {
    	this.tagOwner = tagOwner;
    }

	public void setTagCategories(String[] tagCategories) {
		this.tagCategories = tagCategories;
	}
	
	public String[] getTagCategories() {
		return this.tagCategories;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl  = serverUrl;
	}
	
	public String getServerUrl() {
		return this.serverUrl;
	}
	
	public void setServerPort(String serverPort) {
		this.serverPort  = serverPort;
	}
	
	public String getServerPort() {
		return this.serverPort;
	}
	
	public void setCurrentImg(String img) {
		currentImg = img;
	}
	
	public String getCurrentImg() {
		return currentImg;
	}
	
	public void setIntent(Intent intent) {
		intentlist.push(intent);
	}
	
	public Intent getIntent() {
		if(intentlist.isEmpty())
			return null;
		
		return intentlist.pop();
	}
}
